package com.example.littleken.secret.Activitys;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.littleken.secret.Net.Comment;
import com.example.littleken.secret.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 本类为主页面ListView的适配器
 */
public class AtyMessageCommentListAdaoter extends BaseAdapter {
    private List<Comment> comments = new ArrayList<>();
    private Context context;

    public AtyMessageCommentListAdaoter(Context context) {
        this.context = context;
    }

    public Context getContext() {
        return context;
    }

    @Override
    public int getCount() {
        return (comments.size() > 0)?comments.size():0;
    }

    @Override
    public Comment getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //初始化本适配器
    public void addAll(List<Comment> data){
        comments.addAll(data);
        notifyDataSetChanged();
    }
    //清理适配器所有数据
    public void clear(){
        comments.clear();
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListCell lc;
        Comment comment;
        if(convertView == null){//一级优化
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.aty_timeline_list_cell,null);
            convertView.setTag(new ListCell((TextView) convertView.findViewById(R.id.tvCellLabel)));//二级优化
        }
        lc = (ListCell) convertView.getTag();
        comment = getItem(position);
        lc.getTvCellLable().setText(comment.getContent());
        return convertView;
    }
    //二级优化，viewHodler
    public static class ListCell{
        private TextView tvCellLable;

        public ListCell(TextView tvCellLable) {
            this.tvCellLable = tvCellLable;
        }

        public TextView getTvCellLable() {
            return tvCellLable;
        }
    }
}
